﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Linq;

namespace SMS.Common.Search
{
    public class SearchManager
    {
        //public static ISearchProvider<T> GetProviderFromAttribute<T>(SearchCondition condition)
        //{
        //    object[] ar_obj = condition.GetType().GetCustomAttributes(typeof(SearchExecutorAttribute), false);
        //    if (ar_obj.Length > 0)
        //    {
        //        SearchExecutorAttribute se = ar_obj[0] as SearchExecutorAttribute;
        //        ISearchProvider<T> prov = Activator.CreateInstance(se.ProviderType) as ISearchProvider<T>;
        //        return prov;
        //    }
        //    return null;
        //}

        private static SearchManager sm_Instance;
        public static SearchManager Instance
        {
            get
            {
                if (sm_Instance == null) sm_Instance = new SearchManager();
                return sm_Instance;
            }
        }

        /// <summary>
        /// 搜索并返回结果
        /// </summary>
        /// <param name="dataContext">数据上下文</param>
        /// <param name="condition">搜索条件</param>
        /// <returns>返回搜索结果</returns>
        public SearchResult<T> Search<T>(DataContext dataContext, SearchCondition condition) where T : class
        {
            var searchProvider = new LinqSearchProvider<T>(dataContext);
            return searchProvider.Search(condition);
        }
    }
}
